package com.bifang.common.vo;

import com.bifang.common.interfaces.BaseError;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/** 返回信息 */
@Data
public class AppResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    // 返回代码值 0为成功 <0 错误 >0警告提示
    @ApiModelProperty(notes = "返回代码值 0为成功 <0 错误 >0警告提示")
    private Integer code;

    @ApiModelProperty(notes = "扩展值")
    private String extCode;
    // 返回信息
    @ApiModelProperty(notes = "返回描述信息")
    private String message;
    // 返回的具体内容
    @ApiModelProperty(notes = "返回数据")
    private T result;
    // 返回时间
    @ApiModelProperty(notes = "返回时间戳")
    private Long timestamp;

    public static AppResponse success() {
        AppResponse ar = new AppResponse();
        ar.code = 0;
        ar.message = null;
        ar.result = null;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static AppResponse successMsg(String msg) {
        AppResponse ar = new AppResponse();
        ar.code = 0;
        ar.message = msg;
        ar.result = null;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static <T> AppResponse<T> successData(T data) {
        AppResponse<T> ar = new AppResponse();
        ar.code = 0;
        ar.message = null;
        ar.result = data;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static <T> AppResponse<T> success(String msg, T data) {
        AppResponse<T> ar = new AppResponse();
        ar.code = 0;
        ar.message = msg;
        ar.result = data;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static <T> AppResponse<T> success(String extCode, String msg, T data) {
        AppResponse<T> ar = new AppResponse();
        ar.code = 0;
        ar.extCode = extCode;
        ar.message = msg;
        ar.result = data;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static AppResponse error() {
        AppResponse ar = new AppResponse();
        ar.code = -1;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static AppResponse error(BaseError errorEnum) {
        AppResponse ar = new AppResponse();
        ar.code = errorEnum.getCode();
        ar.extCode = errorEnum.getExtCode();
        ar.message = errorEnum.getMessage();
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static AppResponse errorMsg(String msg) {
        AppResponse ar = new AppResponse();
        ar.code = -1;
        ar.message = msg;
        ar.result = null;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static <T> AppResponse<T> errorData(T data) {
        AppResponse<T> ar = new AppResponse();
        ar.code = -1;
        ar.message = null;
        ar.result = data;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static <T> AppResponse<T> error(String msg, T data) {
        AppResponse<T> ar = new AppResponse();
        ar.code = -1;
        ar.message = msg;
        ar.result = data;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static AppResponse error(Integer code, String msg) {
        AppResponse ar = new AppResponse();
        ar.code = code;
        ar.message = msg;
        ar.result = null;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static <T> AppResponse<T> error(Integer code, String msg, T data) {
        AppResponse<T> ar = new AppResponse();
        ar.code = code;
        ar.message = msg;
        ar.result = data;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    public static <T> AppResponse<T> error(Integer code, String extCode, String msg, T data) {
        AppResponse<T> ar = new AppResponse();
        ar.code = code;
        ar.extCode = extCode;
        ar.message = msg;
        ar.result = data;
        ar.timestamp = System.currentTimeMillis();
        return ar;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}
